//DeepSeek API 使用与 OpenAI 兼容的 API 格式，通过修改配置，您可以使用 OpenAI SDK 来访问 DeepSeek API，或使用与 OpenAI API 兼容的软件。
import OpenAI from "openai";
let  initMessages = [{ role: "system", content: "你是一个文档编辑助手,现在用户选定了部分内容需要你进行优化，请从通顺、逻辑等方面进行优化改写，直接返回优化后的内容." }]
let messages = initMessages

// const deepseek = new OpenAI({
//         baseURL: 'https://api.deepseek.com/v1',
//         apiKey: 'sk-6bc0efcfa8aa402baad3d4b022c0337a',
//         dangerouslyAllowBrowser:true
// });

const deepseek = new OpenAI({
        baseURL: 'https://tbnx.plus7.plus/v1',
        apiKey: 'sk-R0wj1frdFsU1ZWyz19qUOhvLwvtRAuQgipL47ihbBZVZ3LBx',
        dangerouslyAllowBrowser:true
});

async function req(messages,selection,callbackFunction) {
    let range = selection.Range;
  const completion = await deepseek.chat.completions.create({
    messages:messages,
    model: "deepseek-reasoner",
    stream: true
  });
  let fullSingleMsg = ''
  range.InsertParagraphAfter()
  for await (const chunk of completion) {
    let tempConetnet = chunk.choices[0]?.delta?.content || ''
    callbackFunction(tempConetnet,range)
    selection.MoveRight()
    fullSingleMsg+=tempConetnet
  }
  //let respMsg = completion.choices[0].message
  messages.push(fullSingleMsg)
  console.log(fullSingleMsg)
  //todo此处需要补全写入到wps
  //callbackFunction(respMsg.content)
}

function clearMsg(){
    messages = initMessages
}

function chat(selection,callbackFunction){
    let message = {role:"user",content:`${selection.Range.Text}`}
    messages.push(message)
    req(messages,selection,callbackFunction)
}

export default {
    chat,
    clearMsg
}
  